package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.AssessmentInformation;
import tgc.edu.exam.entity.StudentAssess;
import tgc.edu.exam.entity.StudentInformation;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.service.StudentAssessService;
import tgc.edu.exam.service.StudentInformationService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.web.form.AssessmentInformationForm;

@Controller
@RequestMapping(value="/assessmentInformation")
public class AssessmentInformationController extends CommonController<AssessmentInformation, UUID, AssessmentInformationForm> {
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private StudentInformationService informationService;
	
	private StudentAssessService stuassessService;
	
	@Override
	public Specification<AssessmentInformation> buildSpec(AssessmentInformationForm form) {
		Specification<AssessmentInformation> specification = new Specification<AssessmentInformation>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<AssessmentInformation> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getExamination())) {
					Predicate name = cb.like(root.get("examination"), "%"+form.getExamination()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getScoringmethod())) {
					Predicate difficultylevel = cb.like(root.get("scoringmethod"), "%"+form.getScoringmethod()+"%");
					rules.add(difficultylevel);
				}
				if(StringUtils.hasText(form.getTbClassid())) {
					Predicate difficultylevel = cb.like(root.get("tbClassid"), form.getTbClassid());
					rules.add(difficultylevel);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
	
	
	
	
	@Override
	public void manage(ModelMap map) {
		Specification<TbClass> spec  = buildSpec2(null);
		List<TbClass> list = tbClassService.findAll(spec);
		map.put("data", list);
		super.manage(map);
	}

	
	



	@Override
	public void edit(AssessmentInformationForm form, ModelMap map)
			throws InstantiationException, IllegalAccessException {
		map.put("form", form);
		super.edit(form, map);
	}




	@Override
	public Object save(AssessmentInformationForm form) {
		// TODO Auto-generated method stub
		return super.save(form);
	}




	@Override
	public Object delete(AssessmentInformationForm form) {
		List<StudentInformation> list = informationService.findByInformationId(form.getId());
		for (StudentInformation studentInformation : list) {
			StudentAssess  stuassess = studentInformation.getAssess();
			stuassess.setScore(stuassess.getScore()-studentInformation.getGrade());
			if(studentInformation.getGrade()!=0) {
				stuassess.setScore(stuassess.getActualscore()-studentInformation.getGrade());
			}
			stuassessService.save(stuassess);
			informationService.delete(studentInformation);
		}
		return super.delete(form);
	}




	private Specification<TbClass> buildSpec2(SysUser sysUser) {
		Specification<TbClass> spec = new Specification<TbClass>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if(sysUser!=null) {	
					rules.add(cb.equal(root.get("teacherId").get("id"), sysUser.getId()));
				}
				rules.add(cb.equal(root.get("classStatus").get("state"), "开班"));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
}
